/*初始化datagrid*/
$(function () {
    /*抽取常用组件*/
    var dept_list = $("#dept_list");
    var dept_dialog = $("#dept_dialog");
    var dept_form = $("#dept_form");
    /*对象管理方法*/
    var object = {
        add: function () {
            dept_dialog.dialog('setTitle', '添加部门');
            dept_form.form('clear');
            dept_dialog.dialog('open');
        },
        edit: function () {
            var row = dept_list.datagrid('getSelected');
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'info');
            } else {
                dept_form.form('clear');
                dept_form.form('load', row);
                dept_dialog.dialog('setTitle', '编辑部门');
                dept_dialog.dialog('open');
            }
        },
        save: function () {
            dept_form.form('submit', {
                url: '/dept/saveOrUpdate.do',
                success: function (data) {
                    var res = $.parseJSON(data);
                    if (res.success) {
                        $.messager.alert('温馨提示', res.msg, 'info', function () {
                            dept_dialog.dialog('close');
                            dept_list.datagrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示', res.msg, 'error');
                    }
                }
            });
        },
        cancel: function () {
            dept_dialog.dialog('close');
        },
        reload: function () {
            dept_list.datagrid('reload');
        },
        changeState: function () {
            var row = dept_list.datagrid('getSelected');
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'info');
                return;
            }
            $.get('/dept/changeState.do', {state: !row.state, id: row.id}, function (data) {
                var data = $.parseJSON(data);
                if (data.success) {
                    $.messager.alert('温馨提示', data.msg, 'info', function () {
                        dept_dialog.dialog('close');
                        dept_list.datagrid('reload');
                    });
                } else {
                    $.messager.alert('温馨提示', data.msg, 'error');
                }
            })
        }
    }
    /*事件统一绑定*/
    $("[data-cmd]").click(function () {
        var cmd = $(this).data('cmd');
        object[cmd]();
    })

    dept_list.datagrid({
        fit: true,
        rownumbers:true,
        fitColumns: true,
        pagination: true,
        singleSelect: true,
        toolbar: '#dept_toolbar',
        url: '/dept/query.do',
        onClickRow: function (index, row) {
            if (row.state) {
                $("#btn_state").linkbutton({
                    text: '停用'
                })
            } else {
                $("#btn_state").linkbutton({
                    text: '启用'
                })
            }
        },
        columns: [[
            {field: 'name', title: '部门名称', width: 100},
            {field: 'sn', title: '部门代号', width: 100},
            {
                field: 'state', title: '部门状态', formatter: function (value, row, index) {
                return value ? '启用' : '<font color="red">停用</font>';
            }
            }
        ]]
    })

    dept_dialog.dialog({
        closed: true,
        width: 300,
        height: 200,
        buttons: $("#dept_diaButtons"),
    })

})
